#ifndef CTCPCONNECT_V2_H
#define CTCPCONNECT_V2_H

#include <QObject>
#include <QTimer>
#include <QTcpSocket>
#include <QUdpSocket>
#include "module/chttpmanger.h"
#include "module/csynctcpsocket.h"
#include "ccmdparse_v2.h"

#define MODE_UPDATE_NONE   "none"
#define MODE_UPDATE_WEB    "web"
#define MODE_UPDATE_APP    "app"
#define MODE_UPDATE_ALL    "all"

#define REBOOT_INTERVAL_SECONDS 3600*24*2
#define MAX_CONNECT_FAIL_TIMES  20

class CTcpConnect_v2 : public QObject
{
    Q_OBJECT
public:
    explicit CTcpConnect_v2(QObject *parent = NULL);

    bool needAddProjectPrefix();
    void updateDeviceOnline();

    QByteArray readProjectFile();
    void writeProjectFile(QByteArray d);

signals:
    void s_requestQR();

public slots:
    void doConnectToServer();
    void doTcpDataHandle(QString d);
    void doDisconnect();
    void doStateCheck();
    void doGetProject();
    void doReadProjectSetting(QByteArray d);

    void doChangeUpdateMode(QString mode);
    void doControlCmd(QString cmd);

    void doGatewayDeviceStatusUpdate(QString id,QString d);
    void doUpdateDeviceStatus();
    void sendDataToServer(QString data);

    void doConnectToPushServer();
    void doPushDataHandle(QString d);

private:
    bool isSetUpPorject;
    QByteArray _project_data;
    CCmdParse_v2 _cmdParse;
    CSyncTcpSocket *_tcpSocket,*_pushSocket;

    QTimer _aliveTimer,_deviceUpdateTimer,_alldeviceUpdateTimer;
    QTime _lastAliveTime;

    CHttpManger *_http;
    QStringList _updateList;

    QMap<QString,QString> _updateMap,_allMap;
    int _index_all_map;
    bool _need_update_all;
    QString _updateMode,_project_prefix,_device_serial;

    QUdpSocket _udpUpdate;
    QHostAddress _hostServer;
    int _hostPort;
};

#endif // CTCPCONNECT_V2_H
